package com.cz.adapter.chat;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cz.R;
import com.cz.pojo.Chat;

import java.lang.reflect.Field;
import java.util.ArrayList;

public class ChatViewListAdapter extends BaseAdapter {

    //聊天记录
    private ArrayList<Chat> mList;
    private Context mContext;
    private LayoutInflater inflater;
    private ChatViewListAdapter.HolderView holderView;


    public ChatViewListAdapter(Context context, ArrayList<Chat> list) {
        this.mContext = context;
        this.mList = list;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList == null ? null : mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.item_chat, null);
            holderView = new ChatViewListAdapter.HolderView();
            holderView.isMyself = (TextView) convertView.findViewById(R.id.isMyself);
            holderView.notMyself = (TextView) convertView.findViewById(R.id.notMyself);
            holderView.meHead = (ImageView) convertView.findViewById(R.id.me_head);
            holderView.youHead = (ImageView) convertView.findViewById(R.id.you_head);

            convertView.setTag(holderView);
        } else {
            holderView = (ChatViewListAdapter.HolderView) convertView.getTag();
        }
        Chat Chat = mList.get(position);

        //显示逻辑
        //如果是自己发送的消息  右边显示
        //如果是别人的消息 左边显示
        String t1 = holderView.isMyself.getText().toString();
        String t2 = holderView.notMyself.getText().toString();
        System.out.println(t1);
        if (Chat.getIsmyself()==0){
            holderView.notMyself.setText(Chat.getMassage());
            holderView.isMyself.setText("");
            holderView.youHead.setImageResource(showAvatar("bisugui"));
            holderView.meHead.setVisibility(View.INVISIBLE);
        }else{
            holderView.notMyself.setText("");
            holderView.isMyself.setText(Chat.getMassage());
            holderView.meHead.setImageResource(showAvatar("icon"));
        }

        return convertView;
    }
    private class HolderView {
        private TextView isMyself;
        private TextView notMyself;
        private ImageView meHead;
        private ImageView youHead;
    }

    public int showAvatar(String avatar){
        R.drawable drawable = new R.drawable();
        int resId=0;
        Field field= null;
        try {
            field = R.drawable.class.getField(avatar);
            resId=(Integer)field.get(drawable);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return resId;
    }

}